package com.easyadmin.modules.job.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easyadmin.common.quartz.base.BaseQuartz;
import com.easyadmin.modules.job.domain.ScheduleLog;
import com.easyadmin.modules.job.mapper.ScheduleLogMapper;
import com.easyadmin.modules.stock.domain.SysTradeDateEntity;
import com.easyadmin.modules.stock.mapper.SysStockPriceWarnDao;
import com.easyadmin.modules.stock.mapper.SysStocksDayPriceDao;
import com.easyadmin.modules.stock.mapper.SysTradeDateDao;
import com.easyadmin.modules.sys.mapper.SysLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 *  股票数据入库
 *
 *
 */



@Slf4j
@Component("commonTask")
public class CommonTask implements BaseQuartz {

    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    SysTradeDateDao sysTradeDateDao;
    @Autowired
    private SysStocksDayPriceDao sysStocksDayPriceDao;
    @Autowired
    SysStockPriceWarnDao sysStockPriceWarnDao;
    @Autowired
    private ScheduleLogMapper scheduleLogMapper;


    /**
     * 任务实现
     *  期货数据入库
     *
     *
     */
    @Override
    public void run(String params) {
        log.info("Params === >> " + params);
        log.info("当前时间::::" + FORMAT.format(new Date()));

        LocalDate today = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 转换为字符串
        String todayStr = today.format(formatter);
        //判断当日数据是否执行
        List<ScheduleLog> scheduledata= scheduleLogMapper.selectCreateTime(todayStr,"commonTask");
        if(scheduledata.size()>0){
            return;
        }


        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = sdf.format(date);
        SysTradeDateEntity data =sysTradeDateDao.selectOne(new QueryWrapper<SysTradeDateEntity>()
                .lambda().eq(SysTradeDateEntity::getSysDate,dateString));
        log.info("开始执行定时任务");
        if(data!=null){
            String currentPath = System.getProperty("user.dir");
            if(StringUtils.isNotBlank(data.getTradDayFlag()) && data.getTradDayFlag().equals("1")){
                log.info("执行python ");
                try {
                    // 调用Python脚本
                    Process p = Runtime.getRuntime().exec("python "+currentPath+"\\src\\main\\resources\\python\\data_store.py");
                    //SendMsg.SendDingDing("今日执行完成");
                    // 获取脚本执行的结
                    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line;
                    while ((line = in.readLine()) != null) {
                        System.out.println(line);
                    }
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        log.info(" 基础数据入库完成  CommonTask ");
    }
}
